﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.DB;

namespace ScheduleMaker
{
    public partial class smAdmRoomEditForm : Form
    {
        IList<DB.Domain.Core.Equipment> equipments;
        DB.Domain.Core.Classroom classroom;

        public smAdmRoomEditForm(DB.Domain.Core.Classroom _classroom)
        {
            InitializeComponent();
            this.classroom = _classroom;

            lblRoomName.Text = classroom.Name;
            lblRoomDesc.Text = classroom.Description;

            this.getEquipmentList();
            this.roomEquipmentList();            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Czy na pewno chcesz usunąć wybrane wyposażenie?", "Potwierdź usunięcie", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // coś tam xD - gdy usuwamy

                int selectedIndex = lbRoomEquipment.SelectedIndex;
                try
                {
                    DB.Domain.Core.Equipment equipment = classroom.Equipments.ElementAt(selectedIndex);
                    ClassroomDAO classroomDAO = DAOFactory.getClassroomDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                    
                    classroom.Equipments.Remove(equipment);
                    classroomDAO.saveOrUpdate(classroom);

                }
                catch
                {
                    MessageBox.Show("Nie wybrano wyposażenia!", "Błąd " + selectedIndex);
                }
                // odświeżenie listy
                this.roomEquipmentList();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbEquipments.SelectedIndex;
            try
            {
                DB.Domain.Core.Equipment equipment = equipments.ElementAt(selectedIndex);
                ClassroomDAO classroomDAO = DAOFactory.getClassroomDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                if (!classroom.Equipments.Contains(equipment))
                {
                    this.classroom.Equipments.Add(equipment);
                    classroomDAO.saveOrUpdate(classroom);

                }
                else
                {
                    MessageBox.Show("Wyposażenie już dodano!", "Błąd " + selectedIndex);
                }

            }
            catch
            {
                MessageBox.Show("Nie wybrano wyposażenia!", "Błąd " + selectedIndex);
            }
            // aktualizacja listy
            this.roomEquipmentList();
        }

        // Pobieranie - przedmiotów
        private void getEquipmentList()
        {
            lbEquipments.Items.Clear();
            EquipmentDAO equipmentDAO = DAOFactory.getEquipmentDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            equipments = equipmentDAO.getAll();

            foreach (var elem in equipments)
            {
                lbEquipments.Items.Add(elem.Id + ": " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }          
        }

        // Pobieranie wyposażenia podpiętego do sali
        private void roomEquipmentList()
        {
            lbRoomEquipment.Items.Clear();

            DB.ClassroomDAOSchool classroomDAO = new DB.ClassroomDAOSchool();
            //this.classroom = classroomDAO.getById(this.classroom.Id); 

            foreach (var elem in this.classroom.Equipments)
            {
                lbRoomEquipment.Items.Add(elem.Id + ": " + elem.Name);
                //lbRoomEquipment.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
            }


            // Test powtórzeń

            /*
            //this.classroom = classroomDAO.getById(classroom.Id);
            foreach (var elem in classroom.Equipments)
            {
                Console.WriteLine("EQ: " + elem.Id + " Name: " + elem.Name);
            }
             * */
        }

        private void lbRoomEquipment_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
